<template>
  <van-overlay :show="overlayShow" @click="overlayShow = false" z-index="10000000">
    <div class="wrapper" @click.stop>
      <van-loading type="spinner" color="#ffffff" style="margin-right: 10px;" /> 加载ing
    </div>
  </van-overlay>
  <div class="home" v-if="!overlayShow">
    <div class="header">
      <img src="../assets/ui/img/index_top.png" alt="" />
    </div>
    <div class="content" style='margin-top: -20vh;position: relative;'>
      <commonLayout>
        <!-- 简历投递：已投递过的，提示已经投递过了 使用showDialog提示 -->
        <div class="delivery" @click="gotoJoin">
          <img src="../assets/ui/img/resume_btn.png" alt="" />
        </div>
        <div class="func">
          <!-- 参培确认：面试通过开放；志愿选择：进度达到培训中心开放；进度查询：可以直接进入；置灰点击时使用showDialog提示未开放 -->
          <div :class="['func-item', item.status == 0 ? 'none' : '']" v-for="(item, index) in funcList"
            :key="item.label" @click="gotoRouter(item)">
            <span :class="item.url"></span>
            <div>{{ item.label }}</div>
          </div>
        </div>
        <div class="title">加入方田的N个理由</div>
        <van-swipe :loop="false" :width="swipeWidth" class="swiper" :show-indicators="true">
          <van-swipe-item v-for="(item, index) in swiperList" :key="index" class="swiper-item">
            <div class="content_bg">
              <div class="No">
                <span class="iconfont icon-dianzan"></span>
                {{ 'No.' + (+index + 1) }}
              </div>
              <div class="swipe-text">
                <div class="content">
                  {{ item.text }}
                </div>
                <div style="display: flex;justify-content: center;">
                  <img :src="getImageUrl(`index/swiper-${index + 1}.png`)" alt=""
                    style="margin-top: 10px;width: auto;height: 140px;" />
                </div>
              </div>
            </div>
          </van-swipe-item>
        </van-swipe>
        <div style="width: 30%;margin: 30px auto;">
          <img src="../assets/img/logo.png" alt="" srcset="" />
        </div>
      </commonLayout>
    </div>
  </div>

</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import commonLayout from '../components/CommonLayout.vue';
import { getImageUrl } from '../assets/js/common';
import { windowWidth } from 'vant/lib/utils';

import { localUserinfo } from '../assets/js/common';
import { useRouter } from 'vue-router';
import { showToast } from 'vant';
import { useStore } from '../store';
import apis from '../api/index';
const { userIndexStatus } = apis;
const store = useStore();
const swipeWidth = ref(windowWidth.value * 0.8);
interface FuncItem {
  label: string;
  url: string;
  pach: string;
  status: any;
}
const funcList = ref<FuncItem[]>([
  {
    label: '应聘进度',
    url: 'iconfont icon-yingpinjindu2',
    pach: '/Progress',
    status: -1
  },
  {
    label: '参培确认',
    url: 'iconfont icon-canpeiqueren1',
    pach: '/SureJoin',
    status: 1
  },
  {
    label: '志愿选择',
    url: 'iconfont icon-zhiyuanxuanze',
    pach: '/VolChoice',
    status: 2
  },

]);
const swiperList = ref([
  {
    text: '迅猛的发展速度：学生规模在10年内实现了100人次至15万人次的增长',
  },
  {
    text: '广阔的晋升空间：教师岗管理层平均年龄26.6岁，所有员工平均年龄25岁，每年两次晋升机会',
  },
  {
    text: '优秀的教师团队：老师基本毕业于国内知名高校，让最优秀的人进入教育行业，与优秀同行',
  },
  {
    text: '稳定的高薪待遇：教师岗位平均工资18.6万，每人每年6次涨薪机会',
  },
]);
const router = useRouter();
const userInfo = localUserinfo();
const clickStatus = ref({});
const overlayShow = ref(true);
const getUserInfo = async () => {
  if (userInfo != null) {
    try {
      const response = await userIndexStatus({ userId: userInfo.userId });

      if (response.code != '10000') {
        showToast(response.msg);
        return
      }
      clickStatus.value = response.data;
      //if (!localStorage.getItem("clickStatus")) {
      localStorage.setItem("clickStatus", JSON.stringify(clickStatus.value));
      //}
      //store.$state.clickStatus = clickStatus.value;
      store.$patch({
        clickStatus: clickStatus.value
      })
      funcList.value[1].status = clickStatus.value.trainingStatus;
      funcList.value[2].status = clickStatus.value.volunteerStatus;
      const loginUrl = localStorage.getItem("loginUrl") == undefined ? "/" : localStorage.getItem("loginUrl");
      //登录后重新回到首页，然后根据缓存和用户的状态来判定要不要跳到指定页面
      //if (loginUrl && clickStatus.value.trainingStatus == 1) {
      //router.push('/SureJoin')
      //} else if (loginUrl && clickStatus.value.volunteerStatus == 1) {
      //router.push('/VolChoice')
      //}
      overlayShow.value = false;
    } catch (error) {
      console.error('Error fetching user info:', error);
    }
  } else {
    overlayShow.value = false;
    console.log('userInfo.userId is null');
  }
};
onMounted(() => {
  // 监听窗口大小变化，更新 swipeWidth
  getUserInfo();
})

enum Status {
  Pending = 1,//已经提交了
  // 可以根据实际情况添加更多的状态
}
const handleStatus = (status: number, label: string, patch: string) => {
  switch (label) {
    case '参培确认':
      return status === Status.Pending ? '/SureJoin' : patch;
    case '志愿选择':
      return status === Status.Pending ? '/VolResult' : patch;
    default:
      return null;
  }
};
const gotoRouter = (item: any) => {
  const clickStatus = JSON.parse(localStorage.getItem('clickStatus'));
  if (userInfo != null) {
    if (clickStatus == null) {
      showToast('系统维护中！');
      return;
    }
    //登录状态判定
    if (item.status != null) {
      //志愿选择提交的时候，点击直接进入志愿选择结果页
      if (item.label == '参培确认') {
        //1需要填写参培阶段
        if (item.status == 1) {
          router.push(item.pach)
          //router.push("/train/agreement")
        } else if (item.status == 2) {
          //2已填写参培跳到参培首页
          router.push("/train/index")
        } else {
          showToast('暂未开放~');
        }
      } else if (item.label == '志愿选择') {
        //判定状态码跳转对应的页面
        if (item.status == 1) {
          router.push(item.pach);
        } else if (item.status == 2) {
          router.push({
            path: '/delivered',
            query: {
              identifier: 'contract',
            },
          });
        } else {
          showToast('暂未开放~');
        }
      } else {
        router.push(item.pach)
      }
    }
    // else if(item.status == 0){
    //   showToast('等待通过！');
    // }

  } else {
    localStorage.setItem("loginUrl", item.pach)
    showToast('请先登录');
    router.push('/login')
  }

}
const gotoJoin = () => {
  const clickStatus = JSON.parse(localStorage.getItem('clickStatus'));
  if (userInfo != null) {
    if (clickStatus == null) {
      showToast('系统维护中！');
      return;
    }
    if (clickStatus.resumesStatus == 0) {
      //跳到投递简历页面
      router.push("/Join")
    } else {
      //showToast('等待通过！');
      router.push({
        path: '/delivered',
        query: {
          identifier: 'delivered',
        },
      });
      //跳到已投递简历详情页
    }
  } else {
    showToast('请先登录');
    localStorage.setItem("loginUrl", '/join')
    router.push('/login')
  }
}

</script>

<style lang="less" scoped>
img {
  width: 100%;
}

.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #ffffff;
}

:deep(#layout) {
  padding-left: 20px;
  padding-right: 20px;
}

:deep(.van-swipe__indicator--active) {
  width: 10px !important;
  height: 5px !important;
  background: #209EE4;
  border-radius: 8px 8px 8px 8px;
}

:deep(.van-swipe__indicator) {
  background: rgba(32, 158, 228, 0.8);
  width: 5px;
  height: 5px;
}

.content {
  .delivery {
    width: 100%;
    height: 84px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    //background: #ffffff;
    border-radius: 14px;
    gap: 10px;
  }

  .func {
    height: 84px;
    display: flex;
    justify-content: space-between;
    margin: 17px 0;

    .func-item {
      width: 30%;
      height: 90%;
      border-radius: 14px;
      opacity: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 10px 15px;
      gap: 10px;
      font-size: 26rpx;
      font-weight: normal;
      line-height: 18px;
      letter-spacing: 0px;
      background: #ffffff;

      .iconfont {
        font-size: 24px !important;
      }
    }

    .func-item.none {
      //background-color: rgba(32, 158, 228, 0.1);
      color: #A9C7D8;
    }
  }

  .title {
    font-size: 26rpx;
    font-weight: normal;
    line-height: 25px;
    letter-spacing: 0em;
    font-variation-settings: 'opsz' auto;
    color: #212121;
    margin-bottom: 17px;
    font-weight: bold;
  }

  .swipe-text {
    position: absolute;
    top: 50px;
    padding: 0 12px;
  }

  .No {
    font-size: 16px;
    font-weight: bold;
    color: #ffffff;
    position: absolute;
    top: 7px;
    left: 16px;

    .iconfont {
      font-size: 16px !important;
    }
  }

  .swiper {
    box-sizing: border-box;

    .content_bg {
      width: 96%;
      background: url('../assets/ui/img/join_bg.png');
      background-size: 100%;
      background-repeat: no-repeat;
      position: relative;

      .content {
        height: 65px;
        width: 274px;
        line-height: 26px;
        font-size: 26rpx;
      }
    }

    .swiper-item {
      height: 313px;
      display: flex;
      justify-content: center;
    }
  }
}
</style>
